﻿<%@ Control Language="C#" ClassName="PageControlOrder" %>

<script runat="server">


  public string TotalCount
  {
    get { return this.TotalText.Text; }
    set { this.TotalText.Text = value; }
  }

  public string PagesCount
  {
    get { return this.PagesText.Text; }
    set { this.PagesText.Text = value; }
  }

  public int currentPageNumber
  {
    get { return ViewState["currentPageNumber"] == null ? 1 : Convert.ToInt32(ViewState["currentPageNumber"]); }
    set
    {
      ChangePageNumbers(value);
      //選擇頁碼
      ViewState["currentPageNumber"] = value;
    }
  }

  private void ChangePageNumbers(int value)
  {
    int newStartPageNumber = 1;

    int startPageNumber = string.IsNullOrEmpty(Link1.Text) ? 1 : Convert.ToInt32(this.Link1.Text);
    int lastPageNumber = string.IsNullOrEmpty(PagesText.Text) ? 1 : Convert.ToInt32(this.PagesText.Text);

    //當選擇的頁碼大於3頁時
    if (value >= 3 && lastPageNumber >= 5)
    {
      //當選擇的頁碼大於總頁數 - 4時,就由總頁數 - 4當作起始頁碼
      if (value >= (lastPageNumber - 4))
      {
        newStartPageNumber = lastPageNumber - 4;
      }
      else
      {
        //從兩頁前開始show
        newStartPageNumber = value - 2;
      }
    }

    //變更頁碼
    for (int i = 0; i < 5; i++)
    {
      LinkButton lb = this.PlaceHolder1.FindControl("Link" + (i + 1).ToString()) as LinkButton;
      lb.Text = (newStartPageNumber + i).ToString();
    }


    //將正在瀏覽的頁碼選取,其餘清除
    foreach (var item in this.PlaceHolder1.Controls)
    {
      if (item is LinkButton)
      {
        if (((LinkButton)item).Text == value.ToString())
        {
          ((LinkButton)item).CssClass = "current";
        }
        else
        {
          ((LinkButton)item).CssClass = "";
        }
      }
    }
  }

  public delegate void PageSelectedHander(object sender);
  public event PageSelectedHander OnPageSelected;

  protected void LinkButton_Click(object sender, EventArgs e)
  {
    string selectInfo = ((LinkButton)sender).ID.Replace("Link", "");
    int lastNumber = string.IsNullOrEmpty(PagesText.Text) ? 1 : Convert.ToInt32(PagesText.Text);

    switch (selectInfo)
    {
      case "first":
        this.currentPageNumber = 1;
        break;
      case "prev":
        this.currentPageNumber =
            currentPageNumber == 1 ? 1 : currentPageNumber - 1;
        break;
      case "next":
        this.currentPageNumber =
            currentPageNumber == lastNumber ? lastNumber : currentPageNumber + 1;
        break;
      case "last":
        this.currentPageNumber = lastNumber;
        break;
      default:
        this.currentPageNumber = Convert.ToInt32(((LinkButton)sender).Text);
        break;
    }
    this.OnPageSelected(this);
  }

  protected void Page_Load(object sender, EventArgs e)
  {
    if (Page.IsPostBack == false)
    {
      this.currentPageNumber = 1;
      //if (this.PagesCount == "1") { PlaceHolder1.Visible = false; } 
    }
  }

  public void DisplayPageNumber()
  {
    PlaceHolder1.Visible = true;
    foreach (var item in this.PlaceHolder1.Controls)
    {
      if (item is LinkButton)
      {
        (item as LinkButton).Visible = true;
      }
      if (item is Literal)
      {
        (item as Literal).Visible = true;
      }
    }   
    
    if (this.PagesCount == "4")
    {
      Literal5.Visible = false;
      Link5.Visible = false;
    }
    if (this.PagesCount == "3")
    {
      Literal4.Visible = false;
      Link4.Visible = false;
      Literal5.Visible = false;
      Link5.Visible = false;
    }
    if (this.PagesCount == "2")
    {
      Literal3.Visible = false;
      Link3.Visible = false;
      Literal4.Visible = false;
      Link4.Visible = false;
      Literal5.Visible = false;
      Link5.Visible = false;
    }
    if (this.PagesCount == "1" || this.PagesCount == "0") { PlaceHolder1.Visible = false; } 
  }
</script>

<div class="page-nav">
  <div class="page-nav-info">
    <span>共
            <asp:Literal ID="TotalText" runat="server" Mode="PassThrough"></asp:Literal>
      筆訂單 | 
            共
            <asp:Literal ID="PagesText" runat="server"></asp:Literal>
      頁</span>
  </div>
  <div class="page-links">
    <asp:PlaceHolder ID="PlaceHolder1" runat="server">
      <asp:LinkButton OnClick="LinkButton_Click" ID="Linkfirst" class="page-btn first" runat="server"></asp:LinkButton>
      <asp:LinkButton OnClick="LinkButton_Click" ID="Linkprev" class="page-btn prev" runat="server"></asp:LinkButton>
      <asp:LinkButton OnClick="LinkButton_Click" ID="Link1" runat="server">1</asp:LinkButton>      
      <asp:Literal ID="Literal2" runat="server" Text=" / "></asp:Literal>
      <asp:LinkButton OnClick="LinkButton_Click" ID="Link2" runat="server">2</asp:LinkButton>
      <asp:Literal ID="Literal3" runat="server" Text=" / "></asp:Literal>
      <asp:LinkButton OnClick="LinkButton_Click" ID="Link3" runat="server">3</asp:LinkButton>
      <asp:Literal ID="Literal4" runat="server" Text=" / "></asp:Literal>
      <asp:LinkButton OnClick="LinkButton_Click" ID="Link4" runat="server">4</asp:LinkButton>
      <asp:Literal ID="Literal5" runat="server" Text=" / "></asp:Literal>
      <asp:LinkButton OnClick="LinkButton_Click" ID="Link5" runat="server">5</asp:LinkButton>
      <asp:LinkButton OnClick="LinkButton_Click" ID="Linknext" class="page-btn next" runat="server"></asp:LinkButton>
      <asp:LinkButton OnClick="LinkButton_Click" ID="Linklast" class="page-btn last" runat="server"></asp:LinkButton>
    </asp:PlaceHolder>
  </div>
</div>
